package com.android.test.jnidemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/*
D/TAG_JNI (15909): jni-ndk Hello World JNI LOG
D/jni-ndk (15909): the string from JNC C 'Hello World From JNI!!!!!'
D/TAG_JNI (15909): jni-ndk s=1, i=1, l=2193752, f=0.000000, d=1.000000, c=
D/TAG_JNI (15909): jni-ndk c_str:中国
*
* */
//stephen jni learning
public class MainActivity extends AppCompatActivity {
    //访问java的静态方法
    //public static native void calJavaStaticMethod();
    //访问java的实例方法
    //public static native void callJavaInstaceMethod();
    String TAG = "jni-ndk";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String obj = "obj";
        short s = 1;
        long l = 20;
        byte b = 127;
        int[] arr = new int[10];
        byte yuv[] =  {0x21,0x2,0x3,0x4,0x5,0x6,0x7,0x8};
        int rgb[] = {0x1111,0x2222};
        super.onCreate(savedInstanceState);

        //JniText.tianTongAudioStart();

        //JniText.tianTongAudioStop();

        setContentView(R.layout.activity_main);
        Button start = findViewById(R.id.button);
        Button stop = findViewById(R.id.button2);
        Button sendToTiantong = findViewById(R.id.button3);
        Button recieveVoiceStart = findViewById(R.id.button4);
        Button recievrVoiceStop = findViewById(R.id.button5);

        recieveVoiceStart.setOnClickListener(new View.OnClickListener() {
                                                 @Override
                                                 public void onClick(View view) {
                                                     Log.d(TAG,"start startRecieveTianTongAudio");
                                                    JniText.startRecieveTianTongAudio();
                                                     Log.d(TAG,"start startRecieveTianTongAudio end");
                                                 }
                                             }
        );

        recievrVoiceStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG,"start stopRecieveTianTongAudio");
                JniText.stopRecieveTianTongAudio();
                Log.d(TAG,"start stopRecieveTianTongAudio end");
            }
        });

        sendToTiantong.setOnClickListener(new View.OnClickListener() {
                                              @Override
                                              public void onClick(View view) {
                                                  Log.d(TAG,"start sendToTiantong");
                                                  byte audioVoice[] = {0x66, (byte) 0xfd, (byte) 0xcb, (byte) 0xfa};
                                                  JniText.tianTongAudioBFromPc(audioVoice,  audioVoice.length);
                                                  Log.d(TAG,"start sendToTiantong end");
                                              }
                                          }
        );
        start.setOnClickListener( new View.OnClickListener(){
                                      @Override
                                      public void onClick(View view) {
                                          Log.d(TAG,"start button");
                                          JniText.tianTongAudioStart();
                                          Log.d(TAG,"start button end");
                                      }
                                  }
        );

        stop.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Log.d(TAG,"stop button");
                                        JniText.tianTongAudioStop();
                                        Log.d(TAG,"stop button stop");
                                    }
                                }
        );
/*
        for(int i = 0; i < arr.length; i++){
            arr[i] = i;
        }
        String result = JniText.sayHello();
        Log.d(TAG, "the string from JNC C '" + result + "'");
        JniText.test(s, 3, 20, 1.0f, 10.5, 'A', true, b, "中国", obj, new MyClass(), arr);

        int [][] arr_2d = JniText.intInt2DArray(3);
        for(int k = 0; k < 3; k++){
            for(int j = 0; j < 3; j++)
            {
                //Log.d(TAG,"arr_2d[%d][%d] = %d", k,j,arr_2d[k][j]);
                Log.d(TAG,"k:" + k + "j:" + j + "arr_2d[k][j]:" + arr_2d[k][j]);

            }
        }
        //访问本地代码 然后再访问java方法
        JniText.calJavaStaticMethod();
        JniText.callJavaInstaceMethod();
        //c/c++访问 java的静态变量 和实例变量
        ClassField objField = new ClassField();
        objField.setNum(10);
        objField.setStr("Hello");
        //本地代码访问和修改ClassField中的静态属性
        JniText.accessStaticField();
        JniText.accessInstanceField(objField);

        Log.d(TAG,"In Java--->ClassField.num= " + objField.getNUm());
        Log.d(TAG,"In Java --->ClassFied.str = " + objField.getStr());

        JniText.YUVtoRBG(rgb,yuv,2,8);
*/
    }


    public void onCallJniMT(View view) {
        Log.d(TAG, "onCallJniMT");

        JniText.tianTongAudioStart();
        Log.d(TAG, "onCallJniMT end");
    }
    public void onCallJniStop(View view) {
            Log.d(TAG, "onCallJniStop");

            JniText.tianTongAudioStop();
            Log.d(TAG, "onCallJniStop end");
        }

}